package com.fcc.wfly.printhead.controller;


import com.fcc.wfly.common.domain.ErrorHandleConst;
import com.fcc.wfly.printhead.hal.DeviceUtilizationJson;
import com.fcc.wfly.printhead.service.PrintheadService;
import com.fcc.wfly.base.controller.RestApiController;
import com.fcc.wfly.printhead.service.DeviceUtilizationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class GetDeviceUtilizationController extends RestApiController<DeviceUtilizationJson> {

	private static final String API_NAME = "GetDeviceUtilization";
	private static final String API_PARAM_PN = "PN";
	private static final String API_PARAM_SN = "SN";
	private static final String API_PARAM_STARDATE = "startDate";
	private static final String API_PARAM_ENDDATE = "endDate";

	@Autowired
	private PrintheadService printheadService;
	private int diff;

	@RequestMapping(API_NAME)
	public HttpEntity<DeviceUtilizationJson> getDeviceUtilization(HttpServletRequest request, HttpServletResponse response) {
		return process(request, response);
	}

	@Override
	protected String getApiName() {
		return API_NAME;
	}

	@Override
	protected String[] getApiParamNames() {
		return new String[] { API_PARAM_PN, API_PARAM_SN, API_PARAM_STARDATE, API_PARAM_ENDDATE };
	}
	
	@Override
	protected DeviceUtilizationJson process(Map<String, String> params) {
		String PN = params.get(API_PARAM_PN);
		String SN = params.get(API_PARAM_SN);
		String startDate = params.get(API_PARAM_STARDATE);
		String endDate = params.get(API_PARAM_ENDDATE);
		ErrorHandleConst.ParamsValidate(PN,SN,startDate,endDate);
		return process(PN, SN, startDate, endDate);
	}

	/**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * @return
     */

    
	private DeviceUtilizationJson process(String PN, String SN, String startDate, String endDate) {
		DeviceUtilizationJson result = new DeviceUtilizationJson();
		result.setProduct_Number(PN);
		result.setSerial_Number(SN);
		result.setStart_Date(startDate);
		result.setEnd_Date(endDate);
		DeviceUtilizationVo deviceUtilization = printheadService.getDeviceUtilization(PN, SN, //
				startDate.replace("T", " ").replace("Z", ""), endDate.replace("T", " ").replace("Z", ""));
		result.setProductive_Hours(deviceUtilization.getProductiveHours());
		result.setOn_Hours(deviceUtilization.getOnHours());
		return result;
	}
}